% function fap2Di
%  fap2Di.m            initialization
%          25 March 1999
%
% Example of approximation of two 2-dimensional functions
% using backpropagation in a batch mode
%
% ---------------,
% Andrew P. Paplinski,      app@csse.monash.edu.au
% Comp.Sci.& Soft.Eng., http://www.csse.monash.edu.au/~app/
% Monash University, Clayton 3168, [Melbourne,] AUSTRALIA

clear

p = 3 ; % Number of inputs (2) plus the bias input
L = 12; % Number of hidden signals (with bias)
m = 2 ; % Number of outputs

na = 16 ;  N = na^2; nn = 0:na-1;  % Number of training cases

% Generation of the training cases as coordinates of points
% from two 2-D surfaces

% Specification of the sampling grid

X1 = nn*4/na - 2; % na  points from  -2 step (4/na)=.25
                  %            to (2 - 4/na)=1.75
[X1 X2] = meshgrid(X1);  % coordinates of the grid vertices
                         % X1 and X2  are  na by na

R  = (X1.^2 + X2.^2 +1e-5);  % R is a  na by na  matrix of
   % squares of distances of the grid vertices from the origin

D1 = X1 .* exp(-R);  D = (D1(:))';
                  % D1 is na by na, D  is   1 by N

D2 = 0.25*sin(2*R)./R ;  D = [D ; (D2(:))'];
           % D2 is na by na
           % D  is  2 by N  matrix of target output 2D vectors

Y = zeros(size(D)) ;  % memory allocation for the output matrix
X = [ X1(:)'; X2(:)'; ones(1,N) ];  % Appending the bias inputs
                                    % X  is  p by N
figure(1), clf reset, hold off

surfc([X1-2 X1+2], [X2 X2], [D1 D2]), % Two 2-D target functions
                                      % are ploted side by side
title('Two 2-D target functions'), grid on, drawnow
% print -deps2 -f1 fap2Dd.eps

% Initialization of the weight matrices

% Hidden layer weight matrix
 Wh = randn(L-1, p)/p;          % Wh  is L-1 by p

% Output layer weight matrix
 Wy = randn(m, L)/L ;         % Wy  is m by L

C = 100;           % maximum number of training epochs
J = zeros(m, C);   % Initialization of the error function
eta = [0.005 0.2]; % Training gains
